package com.bdqn.Exception;

import com.bdqn.controller.Code;
import com.bdqn.controller.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全站异常处理
 */
@Slf4j
@RestControllerAdvice
public class ProjectExceptionAdvice {

    //处理认证异常:用户名不存在
    @ExceptionHandler(UnknownAccountException.class)
    public Result doUnknownAccountException(UnknownAccountException e){
        log.error(e.getMessage());
        return new Result(Code.UNKNOWNACCOUNT_ERR,false,"用户名不存在!");
    }

    //处理认证异常:密码错误
    @ExceptionHandler(IncorrectCredentialsException.class)
    public Result doIncorrectCredentialsException(IncorrectCredentialsException e){
        log.error(e.getMessage());
        return new Result(Code.INCORRECTCREDENTIALS_ERR,false,"密码错误!");
    }

    //授权异常:权限不匹配
    @ExceptionHandler(UnauthorizedException.class)
    public Result doUnauthorizedException(UnauthorizedException e){
        log.error(e.getMessage());
        return new Result(Code.UNAUTHORIZED_ERR,false,"权限不够!");
    }
}
